<?php ob_start();?>
<html>
<?php 
	$root = $_SERVER['DOCUMENT_ROOT'];
 	include($root.'/standart/head.php');
 	include($root.'/standart/dateHeader.php');
 	include($root.'/standart/checkUser.php');
?>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Start Bootstrap - SB Admin Version 2.0 Demo</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/tinyBox.css" rel="stylesheet" type="text/css" media="screen"/>
    <link href="../css/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" media="screen"/>
	<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    <script type="text/javascript">
    	function validate(evt) {
    		var theEvent = evt || window.event;
    	  	var key = theEvent.keyCode || theEvent.which;
    	  	key = String.fromCharCode( key );
    	  	var regex = /[0-9]|\./;
    	  	if( !regex.test(key) ) {
    	    	theEvent.returnValue = false;
    	    	if(theEvent.preventDefault) theEvent.preventDefault();
    	  	}
    	}

    	function callContent(value){
        	if(value != ""){
        		$("#unit_span").text(value.split("-")[1]);
        		$("#input_unit").val(value.split("-")[1]);
        		$("#inventory_name").val($('#selectBox option:selected').text());
        		$("#inventory_id").val(value.split("-")[0]);
        		$("#inventory_unit").val(value.split("-")[2]);
        		$("#bthInventoryDelete").prop( "disabled", false);
        	}else{
        		$("#unit_span").text('');
        		$("#input_unit").val('');
        		$("#inventory_name").val('');
        		$("#inventory_id").val('');
        		$("#inventory_unit").val('');
        		$("#bthInventoryDelete").prop( "disabled", true );
        	}
    		
    		
    		
    	}
    	function calculateTotalAmount(){
			var quantity = document.getElementById('input_quantity').value;
			var amount = document.getElementById('input_amount').value;
			document.getElementById('input_total').value = (quantity * amount).toFixed(2);
    	}
    	function openNewInventory() {
			pane = document.getElementById('inventoryPane');
			if(pane.style.height == '0px'){
				pane.style.display = 'block';
				$("#inventoryPane").animate({
				    height: "300px"
				}, 500 );
			}else{
				pane.style.display = 'block';
				$("#inventoryPane").animate({
				    height: "0px"
				}, 500 , function() {
					document.getElementById('inventoryPane').style.display = 'none';
				});
			}
		}
		
    	function openNewUnit() {
    		TINY.box.show({url:'addUnit.php',width:400,height:300,opacity:20,topsplit:3});
		}

		function closeBox() {
			TINY.box.hide();
		}

		function saveUnit(sName, name){
			url = 'setUnit.php?SHORT_NAME='+sName+'&NAME='+name;
			$('#invisContentView').text(ajx(url, ''));
			closeBox();	
			getUnitList();
		}

		function getUnitList() {
			url = 'setUnit.php';
			ajx(url, 'setUnitSelectBox');
		}

		function setUnitSelectBox(_htm){
			$('#inventory_unit').html(_htm);
		}
		
		function callInventory(isDelete){
			url = 'setInventory.php?'
				+'UNIT='+$("#inventory_unit").val()
				+'&NAME='+$("#inventory_name").val()
				+'&ID='+$("#inventory_id").val();
			if(isDelete == 'E'){
				url = url+'&DELETE=E'
			}
			$('#invisContentView').html(ajx(url, ''));
			warningMessage();
			openNewInventory();
			getInventoryList();
			callContent('');
		}
		
		function getInventoryList() {
			url = 'setInventory.php';
			ajx(url, 'setInventorySelectBox');
		}

		function setInventorySelectBox(_htm){
			$('#selectBox').html(_htm);
		}
		
		<?php 
		$ten_days = time( ) - 86400 * 10;
		$days_ago = date("d/m/Y", $ten_days);
		?>
		var defDate = "<?php echo date('d/m/Y', time());?>";
		var agoDate = "<?php echo $days_ago;?>";
		function loadPage() {
			getInventoryList();
			getUnitList();
			$('#invoice_date').val(defDate);
			$('#invoice_bgn_date').val(agoDate);
			$('#invoice_end_date').val(defDate);
			$('.datepicker').datepicker();
			listMaterials();
			parent.unloadPageX();
		}

		function callMaterial(isDelete){
			url = 'setMaterial.php?'
				+'input_unit='+$("#input_unit").val()
				+'&inventory_id='+$("#inventory_id").val()
				+'&input_quantity='+$("#input_quantity").val()
				+'&input_amount='+$("#input_amount").val()
				+'&input_total='+$("#input_total").val()
				+'&invoice_no='+$("#invoice_no").val()
				+'&invoice_date='+$("#invoice_date").val()
				+'&ID='+$("#input_id").val();
			if(isDelete == 'E'){
				url = url+'&DELETE=E'
			}
			$('#invisContentView').html(ajx(url, ''));
			warningMessage();
			if($('#insert_id').text() != ''){
				$("#input_id").val($('#insert_id').text());
			}
			listMaterials();
			
		}

		function warningMessage() {
			pane = document.getElementById('invisContentView');
			if(pane.style.height == '0px'){
				pane.style.display = 'block';
				$("#invisContentView").animate({
				    height: "30px"
				}, 1000 );
				setTimeout(function(){warningMessage()}, 5000);
			}else{
				pane.style.display = 'block';
				$("#invisContentView").animate({
				    height: "0px"
				}, 1000 , function() {
					document.getElementById('invisContentView').style.display = 'none';
				});
			}
		}

		function clearForm(){
			$("#input_unit").val('');
			$("#selectBox").val('');
			$("#inventory_name").val('');
			$("#inventory_id").val('');
			$("#inventory_unit").val('');
			$("#input_quantity").val('');
			$("#input_id").val('');
			$("#input_amount").val('');
			$("#input_total").val('');
			$("#invoice_no").val('');
			$("#invoice_date").val(defDate);
		}

		function fillMaterials(_htm){
			$('.table-responsive').html(_htm);
			var table = $('#dataTables-example').dataTable({
	            "language": {
	                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
	            }
	        } );
			$('#dataTables-example tbody').on( 'click', 'tr', function () {
		        if ( $(this).hasClass('selected') ) {
		            $(this).removeClass('selected');
		            clearForm();
		        }else {
			        if(table.$('tr.selected') != null){
		        		table.$('tr.selected').removeClass('selected');
			        }
		            $(this).addClass('selected');
		            row = $(this).find("td");
		            $("#input_unit").val(row[7].innerText);
					$("#selectBox").val(row[1].innerText+"-"+row[7].innerText+"-"+row[5].innerText);
					$("#inventory_name").val(row[4].innerText);
					$("#inventory_id").val(row[1].innerText);
					$("#inventory_unit").val(row[5].innerText);
					$("#input_quantity").val(row[6].innerText);
					$("#input_id").val(row[0].innerText);
					$("#input_amount").val(row[8].innerText);
					$("#input_total").val(row[9].innerText);
					$("#invoice_no").val(row[2].innerText);
					$("#invoice_date").val(row[3].innerText);
		        }
		    } );
		}
		
		function listMaterials(){
			url = 'getMaterials.php?'
				+'begin_date='+$("#invoice_bgn_date").val()
				+'&end_date='+$("#invoice_end_date").val()
				+'&invoice_no='+$("#search_invoice_no").val();
			ajx(url, 'fillMaterials');
		}
    </script>
</head>

<body onload="loadPage()">
    <div id="wrapper">
        <div id="inner-page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Malzeme Girişi</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Alınan Ürünlerin Fatura Girişleri
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-6">
                                    <form role="form" action="setContent.php" method="post" id="submitForm">
                                    	<div class="form-group">
                                            <label>Alınan Malzeme Tipi</label>
                                            <select class="form-control" id="selectBox" name="selectBox" onchange="callContent(this.value)" style="width: 75%">
                                            </select>
                                            <div style="float: right; margin-top: -35px;" >
                                            	<button type="button" id="btnNewInventory" onclick="openNewInventory()" class="btn btn-default">Malzeme Ekle / Sil</button>
                                            </div>
                                        </div>
                                        <div class="panel panel-default" id="inventoryPane" style="height: 0px; display: none;">
					                        <div class="panel-heading" >
					                            Malzeme Tipi Girişi
					                        </div>
					                        <div class="panel-body">
					                            <div class="row">
					                                <div class="col-lg-12">
					                                	<div class="form-group" >
				                                            <label>Malzeme Tipi</label>
				                                            <input class="form-control" name="inventory_name" id="inventory_name" type="text">
				                                            <input type="hidden" id="inventory_id" name="inventory_id">
				                                        </div>
					                                	<div class="form-group" >
				                                            <label>Malzeme Birimi</label>
				                                            <select class="form-control" id="inventory_unit" name="inventory_unit" style="width: 75%">
				                                            </select>
				                                            <div style="float: right; margin-top: -35px;" >
				                                            	<button type="button" id="btnNewUnit" onclick="openNewUnit()" class="btn btn-default">Yeni Birim Ekle</button>
				                                            </div>
				                                        </div>
				                                        <button type="button" class="btn btn-default" onclick="callInventory('H')">Kaydet</button>
				                                        <button type="button" id="bthInventoryDelete" class="btn btn-default" onclick="callInventory('E')" disabled="disabled">Sil</button>
					                                </div>
					                            </div>
					                        </div>
					                    </div>
                                        <div class="form-group" >
                                            <label>Alınan Malzeme Miktarı</label>
                                            <input class="form-control" style="width: 90%;" type="text" onkeypress="validate(event)" id="input_quantity" name="input_quantity" onblur="calculateTotalAmount()">
                                            <input type="hidden" id="input_unit" name="input_unit">
                                            <input type="hidden" id="input_id" name="input_id">
                                            <div style="font-weight: bold; float: right; margin-right: 20px; margin-top: -30px;" id="unit_span"></div>
                                        </div>
                                        <div class="form-group" >
                                            <label>Birim Fiyatı</label>
                                            <input class="form-control" type="text" name="input_amount" onkeypress="validate(event)" id="input_amount" onblur="calculateTotalAmount()">
                                        </div>
                                        <div class="form-group" >
                                            <label>Toplam Fiyat</label>
											<input class="form-control" id="input_total" name="input_total" type="text" placeholder="Toplam Tutar" disabled>
                                        </div>
                                        <div class="form-group" >
                                            <label>Fatura No</label>
                                            <input class="form-control" name="invoice_no" id="invoice_no" type="text">
                                        </div>
                                        <div class="form-group" >
                                            <label>Fatura Tarihi</label>
                                            <input type="text" class="form-control datepicker" id="invoice_date" name="invoice_date" >
                                        </div>
                                        <button type="button" id="btnGetHtml" class="btn btn-default" onclick="callMaterial('H')">Kaydet</button>
                                        <button type="button" id="btnGetHtml" class="btn btn-default" onclick="callMaterial('E')">Sil</button>
                                        <button type="button" id="btnGetHtml" class="btn btn-default" onclick="clearForm()">Temizle</button>
                                    </form>
                                </div>
							</div>
                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <div id="invisContentView" style="display: none; height: 0px;"></div>
                    <!-- /.panel -->
                </div>
            </div>
            <div class="row">
                <!-- /.col-lg-12 -->
                <div class="col-lg-12">
                	<div class="panel panel-default">
                        <div class="panel-heading">
                            Alınan Ürünler
                        </div>
                        <div class="panel-body">
                        	<div class="row">
                                <div class="col-lg-12">
                                	<div class="form-group" style="float: left; margin-right: 20px;">
										<label style="margin-top: 7px;">Başlangıç Fatura Tarihi</label>
									</div>
                                	<div class="form-group" style="float: left;  margin-right: 20px;">
                                        <input type="text" class="form-control datepicker" id="invoice_bgn_date">
									</div>
                                	<div class="form-group" style="float: left;  margin-right: 20px;">
										<label style="margin-top: 7px;">Bitiş Fatura Tarihi</label>
									</div>
                                	<div class="form-group" style="float: left;  margin-right: 20px;">
                                        <input type="text" class="form-control datepicker" id="invoice_end_date">
									</div>
                                	<div class="form-group" style="float: left;  margin-right: 20px;">
										<label style="margin-top: 7px;">Fatura No</label>
									</div>
                                	<div class="form-group" style="float: left;  margin-right: 20px;">
                                        <input type="text" class="form-control" id="search_invoice_no">
									</div>
                                	<button type="button" id="btnGetHtml" class="btn btn-default" onclick="listMaterials()">Listele</button>
                                </div>
                            </div>
                        	<div class="table-responsive">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <!-- Core Scripts - Include with every page -->
    <!-- Page-Level Demo Scripts - Forms - Use for reference -->
</body>
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/ajax.js"></script>
    <script type="text/javascript" src="../js/tinybox.js"></script>
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>
</html>
<?php ob_end_flush(); ?>